<template>
  <div class="update-gender-container">
    <van-picker
      title="标题"
      show-toolbar
      :columns="columns"
      :default-index="gender"
      @confirm="onConfirm"
      @cancel="$emit('closeGenderProup')"
    />
  </div>
</template>

<script>
import { updateUserProfileAPI } from '../../../api/index.js'
export default {
  name: 'updateGender',
  model: {
    prop: 'gender',
    event: 'update-gender'
  },
  props: {
    gender: {
      type: Number,
      default: 0
    }
  },
  data () {
    return {
      columns: ['男', '女']
    }
  },
  methods: {
    // value是选中的columns中的元素,index为索引
    async onConfirm (value, index) {
      this.$toast.loading({
        duration: 0, // 持续展示加载状态弹框的毫秒数,当设置为0时,表示持续加载,需要主动关闭
        message: '加载中...',
        forbidClick: true // 是否禁用背景点击
      })
      try {
        await updateUserProfileAPI({
          gender: index
        })
        this.$emit('update-gender', index)
        // 关闭弹出层
        this.$emit('closeGenderProup')
        this.$toast.success('修改性别成功')
      } catch (error) {
        console.log(error)
        this.$toast.fail('修改性别失败')
      }
    }
  }
}
</script>

<style lang="less" scoped>
.update-gender-container{
}
</style>
